import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutterapp/models/room_list_item_data.dart';
import 'package:flutterapp/page/home/tab_search/filter_bar/data.dart';
import 'package:flutterapp/page/utils/dio_http.dart';
import 'package:flutterapp/page/utils/scopoed_model_helper.dart';
import 'package:flutterapp/page/widgets/room_list_item_widget.dart';
import 'package:flutterapp/page/widgets/search_bar/index.dart';

import 'filter_bar/dilter_chawer.dart';
import 'filter_bar/index.dart';

class TabSearch extends StatefulWidget {
  const TabSearch({Key key}) : super(key: key);

  @override
  _TabSearchState createState() => _TabSearchState();
}

class _TabSearchState extends State<TabSearch> {
  List<RoomListItemData> list = [];
  _onFilterBarChange(FilterBarResult data) async {
    //Uri转义
    var cityId = ScopedModelHelper.getAreaId(context);

    // var area = Uri.encodeQueryComponent(data.areaId);
    // var mode = Uri.encodeQueryComponent(data.rentTypeId);
    // var price = Uri.encodeQueryComponent(data.priceId);
    // //字符串数组
    // var more = Uri.encodeQueryComponent(data.moreIds.join(','));

    var area = data.areaId;
    var mode = data.rentTypeId;
    var price = data.priceId;
    // 户型,朝向,楼层
    var more = data.moreIds.join(',');
    print(cityId);
    print(area);
    print(mode);
    print(price);
    print(more);
    //点击了对应城市,并获取当前状态的所有数据并发送请求
    String url = '/houses/list/?cityId=' +
        '${cityId}&area=${area}&mode=${mode}&price=${price}&more=${more}';
    print(url);
    var res = await DioHttp.of(context).get(url);
    var resMap = json.decode(res.toString());
    //解构数据 返回的是搜索到的列表数据..............................r
    List dataListItem = resMap['body'];
    //返回的数据类型转换
    List<RoomListItemData> resList =
        dataListItem.map((json) => RoomListItemData.fromJson(json)).toList();
    setState(() {
      list = resList;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //右侧栏目
        endDrawer: FilterDrawer(),
        appBar: AppBar(
          actions: [Container()],
          //取消边线
          elevation: 0,
          centerTitle: true,
          title: SearchBarPage(
            showLoaction: true,
            showMap: true,
            onSearch: () {
              Navigator.of(context).pushNamed('search');
            },
          ),
          backgroundColor: Colors.blue,
        ),
        body: Column(
          children: [
            Container(
              height: 40.0,
              child: FilterBar(
                onChange: _onFilterBarChange,
              ),
            ),
            Padding(
              padding: EdgeInsets.only(top: 10.0),
            ),
            Expanded(
              child: ListView(
                //本地先用dataLsit  如果接口搞定就用list
                children: list.map((e) => RoomListItemWidget(e)).toList(),
              ),
            )
          ],
        ));
  }
}
